package com.rover022.flixelgame.world
{
	import com.rover022.flixelgame.element.BaseElement;
	import com.rover022.flixelgame.manager.AssetManager;
	import com.rover022.flixelgame.manager.PlayerManager;
	import com.rover022.flixelgame.manager.SfsNetManager;
	import com.rover022.flixelgame.world.displayobject.FlxLoadImage;
	import com.smartfoxserver.v2.core.SFSEvent;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxRect;
	import org.flixel.FlxSprite;
	import org.flixel.plugin.photonstorm.FlxControl;

	/**
	 * 城镇
	 * @author flashpig
	 *
	 */
	public class CityState extends BaseState
	{
		private var _leftPicId:String = "assets/UI/alpha/HVGA/main_bg_grass_left.jpg";
		private var _rightPicId:String = "assets/UI/alpha/HVGA/main_bg_grass_right.jpg";
		private var _islandPicId:String = "assets/UI/alpha/HVGA/main_bg_mountain.jpg";
		private var _skyPicId:String = "assets/UI/alpha/HVGA/main_bg_sky_left.jpg";
		private var _skyRightId:String = "assets/UI/alpha/HVGA/main_bg_sky_right.jpg";
		private var _cloud_1Id:String = "assets/UI/alpha/HVGA/main_cloud_1.png";
		private var _cloud_2Id:String = "assets/UI/alpha/HVGA/main_cloud_2.png";
		private var _cloud_3Id:String = "assets/UI/alpha/HVGA/main_cloud_3.png";
		private var _NPC1Id:String = "assets/UI/DOTA-HERO/Bone.jpg";
		private var _NPC2Id:String = "assets/UI/DOTA-HERO/Bone.jpg";
		private var _NPC3Id:String = "assets/UI/DOTA-HERO/Bone.jpg";
		private var _NPC4Id:String = "assets/UI/DOTA-HERO/Bone.jpg";
		//
		public var maxStageWidth:Number = 1189 * 2;
		public var maxStageHeight:Number = 615;
		private var _backGroundLay:FlxGroup;
		public var builderLay:FlxGroup;
		public var playBuilding:FlxSprite;
		public var playerGroup:FlxGroup; //玩家图层
		public var skillBuilding:FlxSprite;
		public var defeatBackGroundID:String = ""
		public var stageRect:FlxRect; //显示范围 ,控制范围

		public function CityState()
		{
			super();
		}

		override public function create():void
		{
			super.create();
			SfsNetManager.getInstance().eventSign.add(netHandle);
			initGameLay();
		}

		override public function destroy():void
		{
			FlxControl.clear();
			SfsNetManager.getInstance().eventSign.remove(netHandle);
			super.destroy();
		}

		protected function netHandle(e:SFSEvent):void
		{
			switch (e.type)
			{
				case SFSEvent.USER_ENTER_ROOM:
				{
					break;
				}
				case SFSEvent.USER_EXIT_ROOM:
				{
					break;
				}
				default:
				{
					break;
				}
			}
		}

		/**
		 * 初始化背景图层
		 *
		 */
		public function initBackGroundLay():void
		{
			_backGroundLay = new FlxGroup();
			_backGroundLay.active = true;
			//add sky
			var skySprite:FlxSprite = new FlxSprite();
			skySprite.pixels = AssetManager.getInstance().requestAsset(_skyPicId);
			_backGroundLay.add(skySprite);
			//add island
			var island:FlxSprite = new FlxSprite();
			island.pixels = AssetManager.getInstance().requestAsset(_islandPicId);
			_backGroundLay.add(island);
			//add clouds
			//add left and right
			var leftSprite:FlxSprite = new FlxSprite();
			leftSprite.pixels = AssetManager.getInstance().requestAsset(_leftPicId);
			_backGroundLay.add(leftSprite);
			var rightSprite:FlxSprite = new FlxSprite();
			rightSprite.pixels = AssetManager.getInstance().requestAsset(_rightPicId);
			_backGroundLay.add(rightSprite);
			add(_backGroundLay);
		}

		/**
		 * 1初始化背景
		 * 2初始化角色图层
		 * 3丢一个小男孩主角在里面
		 * 4丢一些NPC
		 */
		public function initGameLay():void
		{
			var spriteImage:FlxLoadImage = PlayerManager.getInstance().makeFlxLoadImage(_leftPicId);
			add(spriteImage);
			spriteImage = PlayerManager.getInstance().makeFlxLoadImage(_rightPicId);
			spriteImage.x = 1189;
			add(spriteImage);
			spriteImage = PlayerManager.getInstance().makeFlxLoadImage(_cloud_1Id);
			spriteImage.x = 100;
			spriteImage.y = 100;
			spriteImage.scrollFactor.x = 0.5
			add(spriteImage);
			spriteImage = PlayerManager.getInstance().makeFlxLoadImage(_cloud_2Id);
			spriteImage.x = 800;
			spriteImage.y = 100;
			spriteImage.scrollFactor.x = 0.5
			add(spriteImage);
			spriteImage = PlayerManager.getInstance().makeFlxLoadImage(_cloud_3Id);
			spriteImage.x = 1400;
			spriteImage.y = 100;
			spriteImage.scrollFactor.x = 0.5
			add(spriteImage);
			playerGroup = new FlxGroup();
			add(playerGroup);
			//添加一个可以控制的小人
			stageRect = new FlxRect(0, 0, maxStageWidth, maxStageHeight);
			var controlRect:FlxRect = new FlxRect(0, 0, 1863, maxStageHeight);
			var hero:BaseElement = PlayerManager.getInstance().makeControlHero(stageRect, controlRect);
			hero.bodyElement.x = 100;
			hero.bodyElement.y = 400;
			addPlayer(hero);
			FlxG.watch(hero.bodyElement, "x");
			//添加场景NPC
			var npcPlay:BaseElement;
			npcPlay = PlayerManager.getInstance().makeNpc({name: "约翰", imgId: _NPC2Id, z: -1});
			npcPlay.setPosition(100, 350);
			npcPlay = PlayerManager.getInstance().makeNpc({name: "约翰", imgId: _NPC2Id, z: -1});
			npcPlay.setPosition(400, 350);
			addPlayer(npcPlay);
			npcPlay = PlayerManager.getInstance().makeNpc({name: "约翰", imgId: _NPC2Id, z: -1});
			npcPlay.setPosition(700, 350);
			addPlayer(npcPlay);
			npcPlay = PlayerManager.getInstance().makeNpc({name: "约翰", imgId: _NPC2Id, z: -1});
			npcPlay.setPosition(1000, 350);
			addPlayer(npcPlay);
		}

		/**
		 * 添加一个玩家进来
		 *
		 */
		public function addPlayer(src:BaseElement):void
		{
			playerGroup.add(src);
			playerGroup.sort("z");
		}

		/**
		 * 移除一个玩家
		 *
		 */
		public function removePlayer():void
		{
		}

		/**
		 * 按照玩家目前数量显示玩家列表
		 *
		 */
		public function initPlayerByList():void
		{
		}

		override public function update():void
		{
			super.update();
		}
	}
}
